package com.hfjava.practice05;

/**
 * @author Administrator
 * @version V1.0
 * @ProjectName:IntelliJ IDEA
 * @PackageName: com.hfjava.practice05
 * @ClassName: SaleTicket
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2019/12/27 9:37
 * @Copyright 大牛版板所有
 */
public class SaleTicket implements Runnable {

    private TrainTicket ticket = new TrainTicket();

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (ticket.getSurplusTicket() < 1){
                break;
            }
            makeSaleTicket();
            System.out.println(Thread.currentThread().getName()+"已经卖票"+(i+1)+"次");
        }
    }


    private synchronized void makeSaleTicket(){
        if (ticket.getSurplusTicket() > 0){
            int tickets = ticket.saleTicket();
            System.out.println(Thread.currentThread().getName()+"卖出火车票一张，剩余车票："+tickets+"张");
        }
        else {
            System.out.println("车票已经卖完！");
        }
    }

}
